import math

def is_prime(n):
    if n <= 1:
        return False
    if n == 2:
        return True
    if n % 2 == 0:
        return False
    for i in range(3, int(math.sqrt(n)) + 1, 2):
        if n % i == 0:
            return False
    return True

def is_palindrome(n):
    s = str(n)
    return s == s[::-1]

def generate_palindromes(length):
    palindromes = []
    if length == 1:
        return list(range(1, 10))
    if length % 2 == 1:
        half = length // 2
        start = 10 ** half
        end = 10 ** (half + 1)
        for i in range(start, end):
            s = str(i)
            palindrome = int(s + s[:-1][::-1])
            palindromes.append(palindrome)
    else:
        return [11] if length == 2 else []
    return palindromes

def find_palindrome_primes(max_limit=10**6):
    palindrome_primes = []
    # 1位数回文素数
    for num in range(2, 10):
        if is_prime(num):
            palindrome_primes.append(num)
    # 2位数回文素数
    if 11 <= max_limit and is_prime(11):
        palindrome_primes.append(11)
    # 奇数位数回文素数(3位及以上)
    length = 3
    while length <= len(str(max_limit)):
        current_pals = generate_palindromes(length)
        for pal in current_pals:
            if pal > max_limit:
                break
            if is_prime(pal):
                palindrome_primes.append(pal)
        length += 2
    return palindrome_primes

if __name__ == "__main__":
    primes = find_palindrome_primes(1e9)
    print("回文素数列表:", primes)
    print("回文素数数量:", len(primes)) 